package com.example.transaction.controller;

import com.example.transaction.service.LogService;
import com.example.transaction.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/trans")
public class UserInfoController1 {
    @Autowired
    private UserService userService;
    @Autowired
    private LogService logService;

    @Transactional(propagation = Propagation.REQUIRED)//引入可以自动处理事务的依赖
    @RequestMapping("/user")
    public String registry(String name,String password) throws IOException {
        userService.registry(name,password);
        log.info("注册成功");
//        try {
//            int a=10/0;
//        }catch (Exception e){
////            log.error(e.getMessage());
////            throw  e;
//            //手动回滚事务
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        }
//        if(true){
//            throw new IOException();
//        }
        logService.insertLog(name,"用户注册");
        log.info("插入成功");


        return "注册成功";
    }
}
